﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RTSoft.Modules;
using System.Data.SqlClient;
using RTSoft.SoureCode.Control.NhanVien;

namespace RTSoft.SoureCode.Control.Login
{
    class loginctrl
    {
        DataProvider provider = new DataProvider();
        SqlCommand cmd = new SqlCommand();
        

        public int isValidate(ref String errConnect, ref String errSQL, String user, String pass) {
            int rs = 0;
            cmd.Connection = provider.Connection(ref errConnect);
            try
            {
                String sql = "select * from nhanvien where username = @user and password = @pass";
                cmd.CommandText = sql;
                cmd.Parameters.AddWithValue("@user", user);
                cmd.Parameters.AddWithValue("@pass", pass);
                rs = Convert.ToInt32(cmd.ExecuteScalar()); 
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
                rs = 0;
            }
            return rs;            
        }
        public nhanvieninfo getEmployee(ref String errConnect, ref String errSQL,String username)
        {
            nhanvieninfo nv = new nhanvieninfo();
            cmd.Connection = provider.Connection(ref errConnect);
            try
            {
                String sql = "select * from nhanvien where username = N'" +username+ "'" ;
                cmd.CommandText = sql;
                //cmd.Parameters.AddWithValue("@user", username);
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read()) {
                    nv.ID_NV = Convert.ToInt32(reader[0]);
                    nv.ID_NHOM = Convert.ToInt32(reader[1]);
                    nv.TEN_NV = reader[3].ToString();
                }                
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;                
            }
            return nv;
        }
    }
}
